package com.ss.android.article.base.feature.detail2.e.a;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;

import com.bytedance.common.utility.c.SharedPreferenceCompat;
import com.bytedance.common.utility.collection.WeakHandler;
import com.bytedance.frameworks.b.a.ComponentsManifest;
import com.bytedance.frameworks.base.mvp.AbsInteractor;
import com.bytedance.frameworks.base.mvp.Interactor;
import com.bytedance.frameworks.base.mvp.View;
import com.ss.android.account.Spipe;
import com.ss.android.account.b.OnAccountRefreshListener;
import com.ss.android.action.ArticleActionHelper;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.app.account.LoginDialogStrategyConfig;
import com.ss.android.article.base.feature.detail.presenter.DetailHelper;
import com.ss.android.article.base.feature.detail2.SSPage;
import com.ss.android.article.base.feature.detail2.article.NewArticleDetailFragment;
import com.ss.android.article.base.feature.detail2.d.NewDetailContext;
import com.ss.android.article.base.feature.detail2.view.DetailView;
import com.ss.android.article.base.feature.detail2.view.NewDetailActivity;
import com.ss.android.article.base.feature.e.ArticleShareHelper;
import com.ss.android.article.base.feature.model.Article;
import com.ss.android.article.base.utils.ArticleHelper;
import com.ss.android.article.browser.R;
import com.ss.android.messagebus.Subscriber;
import com.ss.android.messagebus.SubscriptionRouter;
import com.ss.android.model.AbsArticle;
import com.ss.android.model.ItemType;
import com.ss.android.newmedia.e.ShareHelper;

import java.util.Iterator;
import java.util.List;

public final class CommonBizInteractor extends AbsInteractor implements OnAccountRefreshListener {
    public static final class ShareWaySaver {
        private final NewDetailActivity newDetailActivity;

        public ShareWaySaver(NewDetailActivity arg1) {
            this.newDetailActivity = arg1;
        }

        public final void save(int shareWay) {
            this.newDetailActivity.saveShareWay(shareWay);
        }
    }

    public ArticleShareHelper mArticleShareHelper;
    DetailHelper mDetailHelper;
    AppData mAppData;
    public NewDetailContext mNewDetailContext;
    public ShareWaySaver mShareWaySaver;
    private ArticleActionHelper mArticleActionHelper;
    private Spipe mSpipe;
    private LoginDialogStrategyConfig mLoginDialogStrategyConfig;
    private int k;
    private final WeakHandler mWeakHandler;

    public CommonBizInteractor(Context context, NewDetailContext newDetailContext) {
        super(context);
        this.k = -1;
        this.mWeakHandler = new WeakHandler(new WeakHandler.Callback() {
            public final void handleMsg(Message message) {
                if ((CommonBizInteractor.this.hasView()) && CommonBizInteractor.this.mDetailHelper != null) {
                    CommonBizInteractor.this.mDetailHelper.handleMsg(message);
                }
            }
        });
        new com.ss.android.article.base.feature.detail2.e.a.f(this);
        this.mAppData = AppData.getInstance();
        this.mSpipe = Spipe.inst();
        this.mNewDetailContext = newDetailContext;
        this.mLoginDialogStrategyConfig = new LoginDialogStrategyConfig(context);
    }

    public final void releaseView() {
        super.releaseView();
        SubscriptionRouter.removeSubscriber(this);
        this.mSpipe.b(this);
    }

    public final void onCreate(Bundle bundle, Bundle arg10) {
        super.onCreate(bundle, arg10);
        com.ss.android.article.base.feature.detail2.e.a.d v6 = new com.ss.android.article.base.feature.detail2.e.a.d(this);
        this.mArticleActionHelper = new ArticleActionHelper(this.mContext);
        new ShareHelper(this.mContext, ((DetailView) this.mView).getComponent(), this.mAppData);
        this.mDetailHelper = new DetailHelper(((DetailView) this.mView).customGetActivity(), ItemType.ARTICLE, this.mWeakHandler, this.mArticleActionHelper, "detail");
        this.mDetailHelper.a();
        this.mArticleShareHelper = new ArticleShareHelper(((DetailView) this.mView).customGetActivity(), this.mArticleActionHelper, this.mDetailHelper, 200);
        this.mArticleShareHelper.w = 1;
        this.mArticleShareHelper.mNewArticleDetailTTAndroidObject = null;
        this.mArticleShareHelper.h = new e(this);
        this.mArticleShareHelper.g = v6;
        this.mArticleShareHelper.mExtJsonObj = this.mNewDetailContext.a();
        this.mArticleShareHelper.mCategoryName = this.mNewDetailContext.categoryName;
        String detailSource = mNewDetailContext.detailSource;
        if (android.support.design.a.isTextEmpty(detailSource)) {
            if (mNewDetailContext.listType == 1) {
                if ("__all__".equals(mNewDetailContext.categoryName)) {
                    detailSource = "headline";
                } else if (!android.support.design.a.isTextEmpty(mNewDetailContext.categoryName)) {
                    detailSource = mNewDetailContext.categoryName;
                }
            } else if (mNewDetailContext.listType == 3) {
                detailSource = "search";
            } else if (mNewDetailContext.listType == 4) {
                detailSource = "pgc_list";
            } else if (mNewDetailContext.listType == 2) {
                detailSource = "favorite";
            } else if (mNewDetailContext.listType == 8) {
                detailSource = "read_history";
            } else if (mNewDetailContext.listType == 9) {
                detailSource = "push_history";
            } else if (mNewDetailContext.listType == 10) {
                detailSource = "refresh_history";
            } else {
                detailSource = "";
            }
        }

        mArticleShareHelper.detailSource = detailSource;
    }

    public final void setView(View view) {
        super.setView(view);
        SubscriptionRouter.addSubscriber(this);
        this.mSpipe.addOnAccountRefreshListener(this);
    }

    public final void d() {
        int v1_1;
        int v4 = 2;
        int v2 = 0;
        this.k = -1;
        if (this.mNewDetailContext.article != null && (this.hasView())) {
            SSPage v0 = ((DetailView) this.mView).getSSPage();
            if (v0 != null && (v0.isVisible()) && !(v0 instanceof NewArticleDetailFragment) && ((v0 instanceof com.ss.android.article.base.feature.detail2.dd))) {
                ((com.ss.android.article.base.feature.detail2.dd) v0).a();
            }

            if (this.mNewDetailContext.article != null && !this.mNewDetailContext.article.userRepin && !this.mSpipe.isLogin) {
                LoginDialogStrategyConfig v1 = this.mLoginDialogStrategyConfig;
                String v5 = "detail";
                if (!TextUtils.isEmpty(((CharSequence) v5))) {
                    v1.a();
                    Object v0_1 = ComponentsManifest.findComponent(com.ss.android.account.v2.bb.class);
                    if (v0_1 == null) {
                        v1_1 = 0;
                    } else if (((com.ss.android.account.v2.bb) v0_1).b() != null) {
                        v0_1 = v1.c.get(v5);
                        int v0_2 = v0_1 != null ? ((com.ss.android.article.base.app.account.b) v0_1).a : 0;
                        if (v0_2 == 0) {
                            v1_1 = 0;
                        } else if (v0_2 == 1) {
                            v1_1 = 1;
                        } else if (v0_2 != v4) {
                            v1_1 = 0;
                        } else {
                            SharedPreferences v0_3 = AppData.getAppSettingSp(v1.a);
                            int v6 = v0_3.getInt("sp_favor_" + v5, 0) + 1;
                            SharedPreferences.Editor v0_4 = v0_3.edit();
                            v0_4.putInt("sp_favor_" + v5, v6);
                            SharedPreferenceCompat.commit(v0_4);
                            v0_1 = v1.c.get(v5);
                            if (v0_1 == null) {
                                v1_1 = 0;
                            } else if (((com.ss.android.article.base.app.account.b) v0_1).b == null) {
                                v1_1 = 0;
                            } else if (((com.ss.android.article.base.app.account.b) v0_1).b.length <= 0) {
                                v1_1 = 0;
                            } else {
                                for (v1_1 = 0; true; ++v1_1) {
                                    if (v1_1 >= ((com.ss.android.article.base.app.account.b) v0_1).b.length) {
                                        v1_1 = 0;
                                        break;
                                    } else if (v6 == ((com.ss.android.article.base.app.account.b) v0_1).b[v1_1]) {
                                        v1_1 = v4;
                                        break;
                                    }
                                }
                            }
                        }
                    } else {
                        v1_1 = 0;
                    }
                } else {
                    v1_1 = 0;
                }

                if (v1_1 != 1 && v1_1 != v4) {

                } else {
                    if (this.hasView()) {
                        Bundle bundle = android.support.design.a.createJumpData("title_favor", ((DetailView) this.mView).v());
                        if ((this.mContext instanceof Activity)) {
                            Spipe.a((Activity) this.mContext, bundle);
                        }

                        this.k = v1_1;
                        if (this.hasView()) {
                            ((DetailView) this.mView).cleanFavorIvAnimation();
                            ((DetailView) this.mView).setFavorIconSelected(false);
                        }

                        if (v1_1 != v4) {
                        } else if (!this.mAppData.j()) {
                        } else {
                            this.mAppData.unloginFaverFirst = false;
                        }
                    }
                    v2 = 1;
                }
            }

            label_83:
            if (v2 != 0) {
                return;
            }

            this.e();
        }
    }

    private void e() {
        int v2;
        int v6 = R.drawable.doneicon_popup_textpage;
        this.k = -1;
        if (this.hasView()) {
            SSPage v0 = ((DetailView) this.mView).getSSPage();
            if (v0 != null && (v0.isVisible()) && !(v0 instanceof NewArticleDetailFragment)) {
                v0.onFavorBtnClick();
                return;
            }

            Article v3 = this.mNewDetailContext.article;
            if (v3 == null) {
                return;
            }

            long v4 = this.mNewDetailContext.adId;
            boolean v0_1 = !v3.userRepin ? true : false;
            v3.userRepin = v0_1;
            if (((DetailView) this.mView).getCommentMode() == 0) {
                ((DetailView) this.mView).setFavorIconSelected(v3.userRepin);
            }

            AppData.isNightMode();
            if (v3.userRepin) {
                android.support.design.a.showImageToast(((Interactor) this).mContext, R.string.toast_favor, v6);
                ++v3.repinCount;
                v2 = 4;
                ArticleHelper.a = true;
            } else {
                android.support.design.a.showImageToast(((Interactor) this).mContext, R.string.toast_unfavor, v6);
                --v3.repinCount;
                if (v3.repinCount < 0) {
                    v3.repinCount = 0;
                }

                v2 = 5;
                ArticleHelper.a = false;
            }

            this.mAppData.a = System.currentTimeMillis();
            List v6_1 = this.mSpipe.b();
            if (v3.userRepin) {
                if ((this.mAppData.mShareWhenFavor) && !v6_1.isEmpty()) {
                    this.mArticleActionHelper.a(v2, ((AbsArticle) v3), v4, v6_1);
                } else {
                    this.mArticleActionHelper.a(v2, ((AbsArticle) v3), v4);
                }
            } else {
                this.mArticleActionHelper.a(v2, ((AbsArticle) v3), v4);
            }

            if (!this.mAppData.j()) {
                return;
            }

            if (!this.mNewDetailContext.article.userRepin) {
                return;
            }

            if (this.mSpipe.isLogin) {
                return;
            }

            if (!this.mAppData.j()) {
                return;
            }

            if (this.hasView()) {
                Spipe.a(((DetailView) this.mView).customGetActivity(), android.support.design.a.createJumpData("title_favor", "detail_first_favor"));
            }

            this.mAppData.unloginFaverFirst = false;
        }
    }

    public final void onAccountRefresh(boolean arg6, int arg7) {
        if ((arg6) && (this.mSpipe.isLogin)) {
            if (this.mLoginDialogStrategyConfig != null) {
                mLoginDialogStrategyConfig.a();
                SharedPreferences.Editor editor = AppData.getAppSettingSp(mLoginDialogStrategyConfig.a).edit();
                if (mLoginDialogStrategyConfig.c != null) {
                    Iterator iterator = mLoginDialogStrategyConfig.c.keySet().iterator();
                    while (iterator.hasNext()) {
                        editor.putInt("sp_favor_" + iterator.next(), 0);
                    }
                }
                SharedPreferenceCompat.commit(editor);
            }

            if (this.k < 0) {
                return;
            }

            if (this.mNewDetailContext.article == null) {
                return;
            }

            if (this.k != 2 && this.k != 1) {
                return;
            }

            this.e();
            SubscriptionRouter.c(new com.ss.android.article.base.feature.subscribe.a.a());
        }
    }

    @Subscriber
    public final void onCloseEvent(com.ss.android.account.bus.event.c arg3) {
        if (this.k >= 0 && this.mNewDetailContext.article != null) {
            if (this.k == 2) {
                this.e();
            } else if (this.k == 1) {
                if (this.hasView()) {
                    ((DetailView) this.mView).setFavorIconSelected(false);
                }

                this.k = -1;
            }
        }
    }
}

